package com.egg.auth.controller.web;


import cn.hutool.core.util.ObjectUtil;
import com.egg.auth.entity.dept.Dept;
import com.egg.auth.model.request.web.DeptPostOrPut;
import com.egg.auth.model.response.web.DeptInfo;
import com.egg.auth.model.response.web.DeptTree;
import com.egg.auth.service.dept.IDeptService;
import com.egg.common.core.model.controller.WebController;
import com.egg.common.core.model.response.OptionsResponse;
import com.egg.common.core.model.response.ResponseData;
import com.egg.common.util.JsonUtil;
import com.egg.common.util.OptionalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author east
 */
@Slf4j
@Api(tags = WebController.API_PREFIX + "部门中心")
@RestController
@RequestMapping("/dept")
public class WebDeptController extends WebController {


    @Autowired
    private IDeptService deptService;


    @ApiOperation(value = "部门新增")
    @PostMapping
    public ResponseData<String> deptPost(@RequestBody DeptPostOrPut param) {
        Long id = deptService.add(param);
        return ResponseData.success(id.toString());
    }

    @ApiOperation(value = "部门详情")
    @GetMapping("/{id}/info")
    public ResponseData<DeptInfo> deptInfoGet(@ApiParam(value = "部门ID") @PathVariable String id) {
        Dept dept = deptService.baseGetById(id);
        DeptInfo deptInfo = JsonUtil.objToObj(dept, DeptInfo.class);
        return ResponseData.success(deptInfo);
    }

    @ApiOperation(value = "部门编辑")
    @PutMapping("/{id}")
    public ResponseData<String> deptPut(@PathVariable String id, @RequestBody DeptPostOrPut edit) {
        deptService.edit(id, edit);
        return ResponseData.success(id);
    }

    @ApiOperation(value = "部门树")
    @GetMapping("/tree")
    public ResponseData<List<DeptTree>> deptTreeGet(
            @ApiParam(value = "关键字") @RequestParam(value = "keywords", required = false) String keywords
            , @ApiParam(value = "是否启用") @RequestParam(value = "enabled", required = false) Boolean enabled
    ) {
        List<DeptTree> tree = deptService.tree(
                OptionalUtil.build(keywords), OptionalUtil.build(enabled)
        );
        return ResponseData.success(tree);
    }

    @ApiOperation(value = "部门删除")
    @DeleteMapping
    public ResponseData<String> deptListDel(
            @ApiParam(value = "ID集合", required = true) @RequestParam(value = "ids") Collection<String> ids
    ) {
        deptService.del(
                ids.stream()
                        .filter(o -> ObjectUtil.isNotEmpty(o))
                        .map(o -> Long.valueOf(o))
                        .collect(Collectors.toSet())
        );
        return ResponseData.success();
    }

    @ApiOperation(value = "部门下拉选")
    @GetMapping("/option")
    public ResponseData<List<OptionsResponse>> deptOptionGet() {
        List<DeptTree> tree = deptService.tree(
                Optional.empty(), Optional.empty()
        );
        List<OptionsResponse> list = deptTreeChangeOption(tree);
        return ResponseData.success(list);
    }

    private List<OptionsResponse> deptTreeChangeOption(List<DeptTree> tree) {
        if (ObjectUtil.isEmpty(tree)) {
            return Collections.emptyList();
        }
        List<OptionsResponse> optionList = new ArrayList<>();
        tree.forEach(o -> {
            OptionsResponse option = new OptionsResponse();
            option.setLabel(o.getName());
            option.setValue(o.getId().toString());
            option.setChildren(deptTreeChangeOption(o.getChildren()));
            optionList.add(option);
        });
        return optionList;
    }


}

